// pages/qianke/qianke.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import dayjs from "../../utils/dayjs.min";
import {
    get_selkelist,
    studentElective,
    get_selkelist_data,
    studentDrop
} from '../../api/sele/ke'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        ke: false,
        time: 30 * 60 * 60 * 1000,
        timeData: {},
        datalist: [],

    },
    //计算当前时间到设定时间倒计时
    time(val) {
        let time = this.data.time
        let timeData = this.data.timeData

        //获取当前时间戳



        let now = new Date().getTime()
        //ios兼容
        if (now == "Invalid Date") {
            now = new Date().getTime()
        }


        

        let end = new Date(val.replace(/-/g, "/")).getTime()




        

        //结束时间增加3秒
        end = end + 4 * 1000
        let leftTime = end - now

        this.data.time = leftTime
        this.setData({
            time: parseInt(leftTime)
        })
    },
    //结束
    jieshu() {
        let yuxiuan = []
        this.get_selkelist()

        this.data.show = false,
            this.setData({
                show: false,

            })
    },

    xuan(e) {
        let ke_value = this.data.datalist[e.currentTarget.dataset.index]
        if (ke_value.remain == 0&&!ke_value.is_qian) {
            Notify({
                type: 'warning',
                message: '该课程已经满员'
            });
            return
        } else {
            this.data.ke = true
            this.setData({
                ke: this.data.ke,
                value: this.data.datalist[e.currentTarget.dataset.index]
            })
        }




    },

    //选课
    qian(e) {
        let type = e.currentTarget.dataset.value_type
        let id = e.currentTarget.dataset.value_ke
        if (type) {
            //退课
            Dialog.confirm({
                    title: '提示',
                    message: '确定要退课吗？'
                }).then(() => {
                    studentDrop(id).then(res => {
                        
                        if (res.statusCode == 200) {
                            Notify({
                                type: 'success',
                                message: '退课成功'
                            });
                            this.get_selkelist()
                        } else {
                            Notify({
                                type: 'danger',
                                message: res.data.msg
                            });
                        }
                    })
                })
                .catch(() => {
                    // on cancel
                });
        } else {
            studentElective({
                randomCode: id
            }).then(res => {
                
                if (res.statusCode == 200) {

                    //提示抢课成功
                    Notify({
                        type: 'success',
                        message: res.data.message
                    });
                    this.get_selkelist()
                }
            })
        }


    },



    onChange(e) {


        this.setData({
            timeData: e.detail,
        });

    },
    onClose() {
        this.setData({
            show: false
        });

        //回退
        wx.navigateBack({
            delta: 1
        })
    },






    //判断是否到达选课时间
    async is_time() {
        // let res = await server('GET','/api/auth/is_time')
        let res = 200
        if (res != 200) {
            this.data.show = true
            this.setData({
                show: this.data.show
            })
        }
    },

    get_selkelist() {

        get_selkelist().then(res => {

            if (res.data.statusCode == 400) {
                Dialog.alert({
                    title: '提示',
                    message: '选课暂未开始',
                }).then(() => {
                    //回退
                    wx.navigateBack({
                        delta: 1
                    })
                });
            } else if (res.data.statusCode == 100) {


                const time = res.data.message.replace('T', ' ')

                this.data.show = true
                this.time(time)
                this.setData({
                    show: this.data.show
                })
            } else {
                let yuxiuan = []
                this.data.datalist = res.data.data
                get_selkelist_data().then(res => {
                    yuxiuan = res.data.data
                    

                    this.data.datalist.map((item, index) => {
                        
                        // this.data.datalist[index].is_qian = true
                        yuxiuan.map((item2, index2) => {
                            if (item.courseAndTeacherMsg.courseId == item2.courseId) {
                                
                                this.data.datalist[index].is_qian = true
                            }

                        })
                        setTimeout(() => {
                            this.setData({
                                datalist: this.data.datalist
                            })
                        }, 300)


                    })
                })



                //查找已选课程





            }
            wx.stopPullDownRefresh()
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.is_time()
        this.get_selkelist()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.vibrateShort({
            type:'heavy',
            success: res => {
              console.log(res)
            }
          })
        this.onShow()
        wx.hideLoading()
        //提示刷新成功
        wx.showToast({
            title: '刷新成功',
            icon: 'none',
            duration: 1000
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})